package com.shangxia.dreaming.common.util.excelUtil;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.shangxia.dreaming.common.util.excelUtil.WriteHandler.MyWriteHandler;
import com.shangxia.dreaming.common.util.excelUtil.WriteHandler.MyWriteHandler2;
import com.shangxia.dreaming.common.util.excelUtil.WriteHandler.MyWriteHandler3;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.shangxia.dreaming.common.content.ConditionContent.PATH_Result;


/**
 * @ProjectName: dreaming
 * @ClassName: WriteExcel
 * @Time: 2020/10/5
 * @Author: zhouy
 * @Description:
 * @Experience:
 */

@Slf4j
public class WriteExcel {

    public static Map<Integer, List<Integer>> CHANGE_COLOR_MAP = new HashMap<>();
    public static void exportRangeColumnExcel(List<Integer>[][] arr, Integer sonCode, Integer sumCode, int startColumn, int endColumn,int times) {
        String file = PATH_Result + "第" + (startColumn + "-" + endColumn) + "层_"+System.currentTimeMillis()+".xlsx";
        File file1 = new File(file);
        if (file1.exists()) {
            file1.delete();
        }
        startColumn = startColumn - 1;
        ExcelWriter excelWriter = EasyExcel.write(file).build();
        for (int l = startColumn; l < endColumn; l++) {
            List<List<Integer>> lists = new ArrayList<>();
            for (int h = 0; h < arr.length; h++) {
                List<Integer> list = arr[h][l];
                lists.add(list);
                if (list == null) {
                    continue;
                }
                List<Integer> changeColorColumn = new ArrayList<>();
                if (h < arr.length - 1) {
                    List<Integer> realCodeList = arr[h + 1][l].subList(sumCode - sonCode, sumCode);
                    for (Integer value : list) {
                        if (realCodeList.contains(value)) {
                            //要变色的列
                            changeColorColumn.add(list.indexOf(value));
                        }
                    }
                    //当前层要变色的列
                    CHANGE_COLOR_MAP.put(h, changeColorColumn);
                }
            }
            LongestMatchColumnWidthStyleStrategy myWriteHandler ;
            switch (times){
                case 2:
                    myWriteHandler = new MyWriteHandler2(l);
                    break;
                case 3:
                    myWriteHandler = new MyWriteHandler3(l);
                    break;
                default:
                    myWriteHandler = new MyWriteHandler(l);
                    break;
            }
            WriteSheet writeSheet = EasyExcel.writerSheet(l, "第" + (l + 1) + "层").head(HeadFactory.head(l, sumCode)).registerWriteHandler(myWriteHandler).build();
            // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
            excelWriter.write(lists, writeSheet);
            //清空第l层数据
            lists.clear();
            //清空第l层数据
            CHANGE_COLOR_MAP.clear();
        }
        excelWriter.finish();
    }
}
